#include "ColorView.h"
#include <QResizeEvent>
#include <QPainter>
#include <QPaintEvent>

ColorView::ColorView(QWidget *parent)
	: QWidget(parent)
{
    background.load(":/images/xy.png");
	setAutoFillBackground(true);
}

ColorView::~ColorView()
{

}

void ColorView::resizeEvent(QResizeEvent * event)
{
    QWidget::resizeEvent(event);
	QPalette pal(palette());
	pal.setBrush(QPalette::Window,QBrush(background.scaled(event->size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
	setPalette(pal);
	//reDraw();
}

void ColorView::clear()
{
	cR.clear();
	cG.clear();
	cB.clear();
	QPalette pal(palette());
	pal.setBrush(QPalette::Window,QBrush(background.scaled(this->size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
	setPalette(pal);
}

void ColorView::addColor(QSizeF r, QSizeF g, QSizeF b)
{
	cR.push_back(r);
	cG.push_back(g);
	cB.push_back(b);
}

void ColorView::reDraw()
{
	QPainter painter(this);
	painter.translate(9.5*this->width()/200, 203.5*this->height()/211);
	QPen pen;
	pen.setColor(Qt::red);
	pen.setCapStyle(Qt::RoundCap);
	pen.setWidth(5);
	painter.setPen(pen);
	for (int i = 0; i < cR.size(); i++)
	{
		painter.drawPoint((cR.at(0).width()/0.1)* 24*this->width()/200, -(cR.at(0).height()/0.1)*24*this->height()/211);
	}
	pen.setColor(Qt::green);
	painter.setPen(pen);
	for (int i = 0; i < cG.size(); i++)
	{
		painter.drawPoint((cG.at(0).width()/0.1)* 24*this->width()/200, -(cG.at(0).height()/0.1)*24*this->height()/211);
	}
	pen.setColor(Qt::blue);
	painter.setPen(pen);
	for (int i = 0; i < cB.size(); i++)
	{
		painter.drawPoint((cB.at(0).width()/0.1)* 24*this->width()/200, -(cB.at(0).height()/0.1)*24*this->height()/211);
	}
}

void ColorView::paintEvent(QPaintEvent *)
{
	reDraw();
}
